package io.shallwe._admin.basedata.location;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import com.jfinal.plugin.ehcache.CacheKit;
import io.shallwe.common.model.Location;
import io.shallwe.common.model.Toc;

public class LocationService {

	private Location dao=Location.dao;
	
	public Location findById(String id) {
		return dao.findById(id);
	}

	/**
	 * 列出所有工位信息
	 * @return
	 */
	public List<Location> listAllLocation(){
		List<Location> locations = CacheKit.get("listAllLocation","listAllLocation");
		if(locations==null){
			locations = dao.template("location.listAllLocation").find();
			CacheKit.put("listAllLocation" , "listAllLocation" , locations);
		}
		return locations;
	}

	/**
	 * 编码与工位对照表
	 * @return
	 */
	public Map<String,Location> getLocationMap(){
		List<Location> locations = this.listAllLocation();
		return locations.stream().collect(Collectors.toMap(location->location.getLocationCode(), location -> location));
	}


	public Ret paginate(Integer offset, Integer pageSize, String locationCode, String remark) {
		Integer pageNumber=offset/pageSize+1;
		Kv condition =Kv.by("locationCode", locationCode).set("remark",remark);
		Page<Record> paginate = Db.template("location.paginate",condition).paginate(pageNumber, pageSize);
		return Ret.ok("total", paginate.getTotalRow()).set("rows", paginate.getList());
	}
	
	public Ret save(Location location) {
		if(isLocationCodeExists(location.getLocationCode(),"-1")) {
			return Ret.fail("msg", "工艺点编号已经存在，请输入别的工艺点编号");
		}
		
		if(isRemarkExists(location.getLocationCode(),"-1")) {
			return Ret.fail("msg", "工艺点名称已经存在，请输入别的工艺点名称");
		}
		
		location.setId(StrKit.getRandomUUID());
		location.save();
		return Ret.ok().set("msg","操作成功");
	}
	
	public Ret update(Location location) {
		if(isLocationCodeExists(location.getLocationCode(),location.getId())) {
			return Ret.fail("msg", "工艺点编号已经存在，请输入别的工艺点编号");
		}
		
		if(isRemarkExists(location.getLocationCode(),location.getId())) {
			return Ret.fail("msg", "工艺点名称已经存在，请输入别的工艺点名称");
		}
		
		location.update();
		return Ret.ok().set("msg","操作成功");
	}

	/**
	 * @Description:  更新操作，只更新最大停留时间和最小停留时间
	 * @param:  [id, maxStayTime, minStayTime]
	 * @return: com.jfinal.kit.Ret
	 * @author : gkw
	 */
	public Ret updateStayTime(String id,Integer maxStayTime,Integer minStayTime){
		if(maxStayTime==null || minStayTime == null){
			return Ret.fail("msg","请写全最大停留时间和最小停留时间");
		}
		Location location = findById(id);
		location.setMaxStayTime(maxStayTime);
		location.setMinStayTime(minStayTime);
		if(location.update()){
			return Ret.ok().set("msg","操作成功");
		}else{
			return Ret.fail("msg","操作失败");
		}
	}
	
	public Ret delete(String id) {
		dao.deleteById(id);
		return Ret.ok().set("msg","操作成功");
	}
	
	/**
	 * 工位编号是否存在
	 * @param locationCode
	 * @param id
	 * @return
	 */
	public boolean isLocationCodeExists(String locationCode,String id) {
		locationCode=locationCode.toLowerCase().trim();
		String sql="select top 1 id from t_location where lower(locationCode) = ? and id != ? ";
		return Db.queryStr(sql,locationCode,id)!=null;
	}
	
	
	/**
	 * 工位编号是否存在
	 * @param remark
	 * @param id
	 * @return
	 */
	public boolean isRemarkExists(String remark,String id) {
		remark=remark.toLowerCase().trim();
		String sql="select top 1 id from t_location where lower(remark) = ? and id != ? ";
		return Db.queryStr(sql,remark,id)!=null;
	}
	
	public List<Location> getAllLocations(){
		return dao.find("select * from t_location order by startPosition");
	}

	/**
	 * @Description:   参数为有效
	 * @param:  [id]
	 * @return: com.jfinal.kit.Ret
	 * @author : gkw
	 */
	public Ret doControl(String id){
		int n = Db.update("update t_location set control_flag = ? where id=?", Toc.do_control, id);
		if (n > 0) {
			return Ret.ok("msg", "成功");
		} else {
			return Ret.fail("msg", "失败");
		}
	}

	/**
	 * @Description:  参数为无效
	 * @param:  [id]
	 * @return: com.jfinal.kit.Ret
	 * @author : gkw
	 */
	public Ret notControl(String id){
		int n = Db.update("update t_location set control_flag = ? where id=?", Toc.not_control, id);
		if (n > 0) {
			return Ret.ok("msg", "成功");
		} else {
			return Ret.fail("msg", "失败");
		}
	}
}
